#include "progbar.h"

//VERSION1
//void progress() 
//{
//    int i = 0;
//    char buffer[MAX];
//    memset(buffer, 0, sizeof(buffer));
//
//    while(i <= 100)
//    {
//        buffer[i] = HEAD;
//        if(i==100)
//            buffer[i] = FLAG;
//        printf("[\033[30;44m%-100s\033[0m][\033[31m%3d%%\033[0m]\r", buffer, i);
//        fflush(stdout);
//        usleep(100000);
//        buffer[i] = FLAG;
//        ++i;
//    }
//    printf("\n");
//}

//VERSION2

void progress(double rate)
{
    static char buffer[MAX] = {0};
    static char* label = "/-\\|";
    static int cnt = 0;
    int irate = (int)rate;
    if(fabs(rate-100.0) > 1e-6)
        buffer[irate] = HEAD;
    printf("[\033[30;43m%-100s\033[0m][\033[1;31m%.1lf%%\033[0m][%c]\r", buffer, rate, label[(cnt++)%4]);
    fflush(stdout);
    
    buffer[irate] = FLAG;
    if(fabs(rate-100.0) <= 1e-6)
        printf("\n");
}
